package com.luban.springboot7.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import com.luban.springboot7.interceptor.UserInterceptor;

@Configuration
public class UserMvcConfig extends WebMvcConfigurationSupport{
	
	@Autowired
	UserInterceptor userInterceptor;
	
	/**
	 * 根据spring boot 通用配置 静态文件不需拦截
	 */
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");
        super.addResourceHandlers(registry);
    }
	
	/**
	 * 设置拦截规则
	 */
	public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(userInterceptor)
                //addPathPatterns 用于添加拦截规则
                .addPathPatterns("/**");
                //如果有地址不需要拦截用excludePathPatterns排除
        		//例如：.addPathPatterns("/**").excludePathPatterns("/login");
        super.addInterceptors(registry);
    }
}
